#!/bin/sh
set -e



DEFAULT_DOWNLOAD_URL="https://gitee.com/dwdyoung/g-environment"
if [ -z "$EN_DOWNLOAD_URL" ]; then
	EN_DOWNLOAD_URL=$DEFAULT_DOWNLOAD_URL
fi

DEFAULT_INSTALL_PATH="/usr/bin/en_git"
if [ -z "$INSTALL_PATH" ]; then
	INSTALL_PATH=$DEFAULT_INSTALL_PATH
fi

command_exists() {
	command -v "$@" > /dev/null 2>&1
}


is_dry_run() {
	if [ -z "$DRY_RUN" ]; then
		return 1
	else
		return 0
	fi
}


## 是否为windows操作系统
is_wsl() {
	case "$(uname -r)" in
	*microsoft* ) true ;; # WSL 2
	*Microsoft* ) true ;; # WSL 1
	* ) false;;
	esac
}


## 是否为mac操作系统
is_darwin() {
	case "$(uname -s)" in
	*darwin* ) true ;;
	*Darwin* ) true ;;
	* ) false;;
	esac
}



## 获取操作系统类型
get_distribution() {
	lsb_dist=""
	# Every system that we officially support has /etc/os-release
	if [ -r /etc/os-release ]; then
		lsb_dist="$(. /etc/os-release && echo "$ID")"
	fi
	# Returning an empty string here should be alright since the
	# case statements don't act unless you provide an actual value
	echo "$lsb_dist"
}


do_install() {
	echo "# Executing docker install script, commit: $SCRIPT_COMMIT_SHA"

    # en 命令已安装
	if command_exists en; then
		cat >&2 <<-'EOF'
			你已经成功安装了 en 工具，点击 Ctrl+C 退出安装程序。
		EOF
		( set -x; sleep 20 )
		exit 1
	fi

	user="$(id -un 2>/dev/null || true)"

	sh_c='sh -c'
	if [ "$user" != 'root' ]; then
		if command_exists sudo; then
			sh_c='sudo -E sh -c'
		elif command_exists su; then
			sh_c='su -c'
		else
			cat >&2 <<-'EOF'
            	你需要使用root权限执行该命令，当前找不到 sudo 和 su 命令，你可以点击 Ctrl+C 退出安装程序。
			EOF
			( set -x; sleep 20 )
			exit 1
		fi
	fi

	if is_dry_run; then
		sh_c="echo"
	fi

	# perform some very rudimentary platform detection
	lsb_dist=$( get_distribution )
	lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"


    ## TODO 如果是windows平台
	if is_wsl; then
		echo
		echo "检测到windows环境"
		echo
        # 下载git.exe
        # 下载python离线版
        # 拉取en仓库
        # 拉取配置环境变量
		cat >&2 <<-'EOF'
			安装完成，你可以点击 Ctrl+C 退出安装程序。
		EOF
		( set -x; sleep 20 )
        exit 0
	fi


    ## 根据不同平台做不同处理
	case "$lsb_dist" in

        # 下载git
        # 下载python离线版
		ubuntu|debian|raspbian|kylin)
		    # if zhanghao == 'root':
		    #   sh_c = '';
		    # else
		    #   sh_c = 'sudo'
            $sh_c 'apt install git python3 -y'
            $sh_c 'apt install python3-pip -y'
            $sh_c 'pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple'
            $sh_c 'pip3 install pyyaml'
            # $sh_c 'apt install network-manager -y'
            
		;;

		centos|rhel|sles)
			$sh_c 'yum install git python3 -y'
            $sh_c 'yum install python3-pip -y'
            $sh_c 'pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple'
            $sh_c 'pip3 install pyyaml'
		;;

		*)
			$sh_c 'apt install git python3 -y'
            $sh_c 'apt install python3-pip -y'
            $sh_c 'pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple'
            $sh_c 'pip3 install pyyaml'
		;;

	esac

    # 拉取代码
    $sh_c "git clone $EN_DOWNLOAD_URL $INSTALL_PATH"

    # 设置环境变量
	cat >> update_environment.sh <<- 'FFFF'
	cat >> /usr/bin/en <<- 'OOOO'
	EN_PATH=/usr/bin/en_git
	PYTHONDONTWRITEBYTECODE=1 exec python3 "$EN_PATH/en.py" "$@"
	OOOO
	chmod 777 /usr/bin/en
	FFFF
	chmod 777 update_environment.sh
	$sh_c "./update_environment.sh"
	$sh_c "rm update_environment.sh"
	$sh_c "en init"
    
	cat >&2 <<-'EOF'
		安装完成，你可以点击 Ctrl+C 退出安装程序。
	EOF
	( set -x; sleep 20 )
	exit 0
}

do_install